/**
 * @Copyright(C) 2008 Software Engineering Laboratory (SELAB), Department of Computer
 * Science, SUN YAT-SEN UNIVERSITY. All rights reserved.
 **/

package parser;

import exceptions.ExpressionException;
import personal.SimpleFactory;
import personal.parser.MyParser;
import personal.scanner.MyScanner;

import java.util.List;

/**
 * Main program of the expression based calculator ExprEval
 *
 * @author [PENDING your name]
 * @version 1.00 (Last update: [PENDING the last update])
 **/
public class Calculator {
    /**
     * The main program of the parser. You should substitute the body of this method
     * with your experiment result.
     *
     * @param expression user input to the calculator from GUI.
     * @return if the expression is well-formed, return the evaluation result of it.
     * @throws ExpressionException if the expression has error, a corresponding
     * exception will be raised.
     **/
    static MyScanner myScanner = new SimpleFactory().makeScanner();
    static MyParser myParser = new SimpleFactory().makeParser();

    public double calculate(String expression) throws ExpressionException {
        // You should substitute this method body ...
//        personal.scanner.MyScanner myScanner = new SimpleScanner();
//        List<personal.scanner.MyScanner.Token> tokens = myScanner.scan(expression);
//        for (MyScanner.Token token : tokens)
//            System.out.println(token);
        List<MyScanner.Token> tokens = myScanner.scan(expression);
        return Double.parseDouble(myParser.parser(tokens));
//        double result = ((int) (Math.random() * 1000000000)) / 100.0;
//        return result;
    }
}
